#include "zscanform.h"
#include "ui_zscanform.h"
#include <QPainter>
#include <QStyleOption>
#include <QComboBox>
#include <QLabel>

ZScanForm::ZScanForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ZScanForm)
{
    ui->setupUi(this);
    this->setProperty("form","backgroundform");
    ui->leftBorderForm->SetLabelIcon("zScan");
    this->setFixedHeight(370);
    
    // 连接 comboBox 的 currentIndexChanged 信号，使用 QOverload 明确指定重载版本
    connect(ui->comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &ZScanForm::on_comboBox_currentIndexChanged);
    
    // 初始化标签显示，使其与滑块初始值一致
    int initialValue = ui->horizontalSlider_gainSpeed->value();
    ui->label_gainSpeed->setText(QString::number(initialValue));
    ui->label_speed->setText(QString::number(initialValue) + "x");
    ui->label_step->setText(QString::number(initialValue) + "um");
    
    // 设置 lineEdit_range 为不可编辑
    ui->lineEdit_range->setReadOnly(true);
    
    // 连接 lineEdit_rangeT 和 lineEdit_rangeB 的 textChanged 信号到槽函数
    connect(ui->lineEdit_rangeT, &QLineEdit::textChanged, this, &ZScanForm::updateRangeValue);
    connect(ui->lineEdit_rangeB, &QLineEdit::textChanged, this, &ZScanForm::updateRangeValue);
    
    // 连接信号槽
    connect(ui->lineEdit_range, &QLineEdit::textChanged, this, &ZScanForm::updatePlanesCount);
    
    // 初始化计算 range 的值
    updateRangeValue();
    
    // 初始化计算 planes 的值
    updatePlanesCount();
}

ZScanForm::~ZScanForm()
{
    delete ui;
}

void ZScanForm::UpdateShowType(int type)
{
    ui->leftBorderForm->UpdateShowType(type);
}

void ZScanForm::paintEvent(QPaintEvent *event)
{
    QStyleOption opt;
    opt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget,&opt,&painter,this);
    QWidget::paintEvent(event);
}

void ZScanForm::on_toolButton_contractExpand_clicked(bool checked)
{
    ui->widget_hide->setHidden(!checked);
    if(checked)
        this->setFixedHeight(370);
    else
        this->setFixedHeight(50);
}

void ZScanForm::on_comboBox_currentIndexChanged(int index)
{
    // 当 comboBox 切换时，更新 label_ra 的文本
    if(index == 0) // 相对
    {
        ui->label_ra->setText("|相对（Ls）");
    }
    else if(index == 1) // 绝对
    {
        ui->label_ra->setText("|绝对（Ls）");
    }
}

void ZScanForm::on_horizontalSlider_gainSpeed_valueChanged(int value)
{
    // 当滑块值变化时，更新所有相关标签的值
    // 更新速度系数显示
    ui->label_gainSpeed->setText(QString::number(value));
    
    // 更新速度显示
    ui->label_speed->setText(QString::number(value) + "x");

    updatePlanesCount();
    
    // 更新步长显示
//    ui->label_step->setText(QString::number(value) + "um");
}

void ZScanForm::updateRangeValue()
{
    // 获取 lineEdit_rangeT 和 lineEdit_rangeB 的值
    QString rangeTText = ui->lineEdit_rangeT->text();
    QString rangeBText = ui->lineEdit_rangeB->text();
    
    // 尝试将文本转换为浮点数
    bool okT, okB;
    double rangeT = rangeTText.toDouble(&okT);
    double rangeB = rangeBText.toDouble(&okB);
    
    // 只有当两个值都能成功转换为数字时才计算差值
    if (okT && okB)
    {
        double range = rangeT - rangeB;
        ui->lineEdit_range->setText(QString::number(range));
    }
    else
    {
        ui->lineEdit_range->setText("");
    }
}

void ZScanForm::updateStepFromMagnification(int magnification) {
    QString stepText;
    switch (magnification) {
        case 5: stepText = "12um"; break;
        case 10: stepText = "2um"; break;
        case 20: stepText = "1um"; break;
        case 50: stepText = "0.2um"; break;
        case 100: stepText = "0.2um"; break;
        default: stepText = "0um"; break;
    }
    ui->label_step->setText(stepText);
    updatePlanesCount();
}

void ZScanForm::updatelabelChanged(const QString &label)
{
    if(0 == ui->comboBox->currentIndex())
    {
        if(!ui->pushButton_tRange->isChecked())
            ui->lineEdit_rangeT->setText(label);
        if(!ui->pushButton_bRange->isChecked())
            ui->lineEdit_rangeB->setText(label);
    }
}

void ZScanForm::updatePlanesCount() {
    // 获取 lineEdit_range 的值
    QString rangeText = ui->lineEdit_range->text();
    bool okRange;
    double range = rangeText.toDouble(&okRange);

    // 获取 label_speed 的值（去掉 "x" 后缀）
    QString speedText = ui->label_speed->text().replace("x", "");
    bool okSpeed;
    double speed = speedText.toDouble(&okSpeed);

    // 获取 label_step 的值（去掉 "um" 后缀）
    QString stepText = ui->label_step->text().replace("um", "");
    bool okStep;
    double step = stepText.toDouble(&okStep);

    // 计算 planes 的值
    if (okRange && okSpeed && okStep && step != 0) {
        double planes = range / (speed * step);
        int result = (planes > static_cast<int>(planes)) ?
                    static_cast<int>(planes) + 1 :
                    static_cast<int>(planes);
        ui->label_planes->setText(QString::number(result));
    } else {
        ui->label_planes->setText("0");
    }
}
